package com.haonan.springboot_exercise.controller;

import com.haonan.springboot_exercise.bean.User;
import com.haonan.springboot_exercise.bean.pojo.Result;
import com.haonan.springboot_exercise.dao.AdminDao;
import com.haonan.springboot_exercise.dao.ServerLogDao;
import com.haonan.springboot_exercise.service.AdminService;
import com.haonan.springboot_exercise.service.PopServerService;
import com.haonan.springboot_exercise.service.SmtpServerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author haonan
 * @date 2021/5/13 15:06
 **/
@RestController
@CrossOrigin(origins = "http://localhost:9527",maxAge = 1800)//解决跨域问题
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    PopServerService popServerService;
    @Autowired
    SmtpServerService smtpServerService;
    @Autowired
    ServerLogDao serverLogDao;
    @Autowired
    AdminDao adminDao;

    /**
     * 切换到管理员模式
     * @param user 用户实体
     * @return Result 20000代表成功 40000代表失败
     */
    @RequestMapping("/usermanagement/admin")
    public Result switchToAdmin(@RequestBody User user){
        return adminService.switchToAdmin(user);
    }

    /**
     * 改变用户的身份权限
     * @param user_name 用户名
     * @param role_id 权限 0是普通用户， 1是管理员
     * @return
     */
    @RequestMapping("/usermanagement/changeRole")
    public Result changeRoleOfUser(@RequestParam("user_name") String user_name, @RequestParam("role_id") Boolean role_id){
        return adminService.changeRoleOfUser(user_name, role_id);
    }

    /**
     * 改变用户的可用禁用状态
     * @param user_name
     * @param state_flag 0禁用 1可用
     * @return
     */
    @RequestMapping("/usermanagement/changeState")
    public Result changeStateOfUser(@RequestParam("user_name")String user_name, @RequestParam("state_flag") Boolean state_flag){
        return adminService.changeStateOfUser(user_name, state_flag);
    }

    /**
     * 改变用户的Smtp可用状态
     * @param user_name
     * @param smtp_flag 0禁用 1可用
     * @return
     */
    @RequestMapping("/usermanagement/changeSmtpFlag")
    public Result changeSmtpFlagOfUser(@RequestParam("user_name") String user_name, @RequestParam("smtp_flag")Boolean smtp_flag){
        return adminService.changeSmtpFlagOfUser(user_name, smtp_flag);
    }

    /**
     * 改变用户的Pop可用状态
     * @param user_name
     * @param pop_flag 0禁用 1可用
     * @return
     */
    @RequestMapping("/usermanagement/changePopFlag")
    public Result changePopFlagOfUser(@RequestParam("user_name") String user_name, @RequestParam("pop_flag") Boolean pop_flag){
        return adminService.changePopFlagOfUser(user_name, pop_flag);
    }

    /**
     * 改变用户 用户名和密码
     * @param user_name 旧用户名
     * @param new_user_name 新用户名
     * @param new_user_password 新密码
     * @return
     */
    @RequestMapping("/usermanagement/changeInfo")
    public Result changeInfoOfUser(@RequestParam("user_name") String user_name, @RequestParam("new_user_name")String new_user_name, @RequestParam("new_user_password")String new_user_password){
        return adminService.changeInfoOfUser(user_name, new_user_name, new_user_password);
    }

    /**
     * 创建用户
     * @param user
     * @return
     */
    @RequestMapping("/usermanagement/createUser")
    public Result createNewUser(@RequestBody User user){
        return adminService.createNewUser(user);
    }

    /**
     * 删除用户
     * @param user_name
     * @return
     */
    @RequestMapping("/usermanagement/deleteUser")
    public Result deleteUser(@RequestParam("user_name") String user_name){
        return adminService.deleteUser(user_name);
    }

    @RequestMapping("/usermanagement/getUser")
    public Result getUser(@RequestParam("user_name") String user_name){
        return adminService.getUser(user_name);
    }

    /**
     * 通过用户名模糊查询得到符合结果的集合
     * @param fuzzy_user_name 模糊的用户名
     * @return Result，其中data是User集合
     */
    @RequestMapping("/usermanagement/getUserByFuzzy")
    public Result getUserByFuzzy(@Param("fuzzy_user_name")String fuzzy_user_name){
        return adminService.getUserByFuzzy(fuzzy_user_name);
    }

    @RequestMapping("/usermanagement/getAllUser")
    public Result getAllUser(){
        return adminService.getAllUser();
    }

    /**
     * 获取Pop服务状态
     * @return
     */
    @RequestMapping("/usermanagement/getPopState")
    public Result getPopState(){
        return adminService.getPopState();
    }

    /**
     * 获取Smtp服务状态
     * @return
     */
    @RequestMapping("/usermanagement/getSmtpState")
    public Result getSmtpState(){
        return adminService.getSmtpState();
    }

    /**
     * 获取Pop服务端口
     * @return
     */
    @RequestMapping("/usermanagement/getPopPort")
    public Result getPopPort(){
        return adminService.getPopPort();
    }

    /**
     * 获取Smtp服务端口
     * @return
     */
    @RequestMapping("/usermanagement/getSmtpPort")
    public Result getSmtpPort(){
        return adminService.getSmtpPort();
    }


    /**
     * 关闭Pop服务
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/closePopServer")
    public Result closePopServer(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "关闭了Pop服务");
        return popServerService.closePopServer();
    }

    /**
     * 关闭Smtp服务
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/closeSmtpServer")
    public Result closeSmtpServer(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "关闭了Smtp服务");
        return smtpServerService.closeSmtpServer();
    }

    /**
     * 开启Pop服务
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/openPopServer")
    public Result openPopServer(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "开启了Pop服务");
        return popServerService.openPopServer();
    }

    /**
     * 开启Smtp服务
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/openSmtpServer")
    public Result openSmtpServer(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "开启了Smtp服务");
        return smtpServerService.openSmtpServer();
    }

    /**
     * 关闭系统服务，即关闭Smtp和Pop
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/closeSystem")
    public Result closeSystem(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "关闭了系统服务");
        return adminService.closeSystem();
    }

    /**
     * 开启系统服务，即开启Smtp和Pop
     * @return
     * @throws Exception
     */
    @RequestMapping("/usermanagement/openSystem")
    public Result openSystem(@RequestParam("admin_name") String admin_name) throws Exception {
        serverLogDao.recordServerLog(admin_name, "开启了系统服务");
        return adminService.openSystem();
    }

    /**
     * 修改Pop服务端口号
     * @param port
     * @return
     */
    @RequestMapping("/usermanagement/changePopPort")
    public Result changePopPort(@RequestParam("admin_name") String admin_name, @RequestParam("port") int port) throws Exception {
        serverLogDao.recordServerLog(admin_name, "将端口号由"+adminService.getPopPort().getData()+"修改为"+port);
        return adminService.changePort("pop", port);
    }

    /**
     * 修改Smtp服务端口号
     * @param port
     * @return
     */
    @RequestMapping("/usermanagement/changeSmtpPort")
    public Result changeSmtpPort(@RequestParam("admin_name") String admin_name, @RequestParam("port") int port) throws Exception {
        serverLogDao.recordServerLog(admin_name, "将端口号由"+adminService.getSmtpPort().getData()+"修改为"+port);
        return adminService.changePort("smtp", port);
    }

    /**
     * 获取到系统日志列表 , 返回在Result的Data里面
     * 参数是 Mysql 语法里面 Limit 的两个参数
     * @param pageStart 返回记录行的偏移量
     * @param pageSize 返回记录行的最大数目
     * @return
     */
    @RequestMapping("/usermanagement/getServerLogList")
    public Result getServerLogList(@RequestParam("pageStart") int pageStart, @RequestParam("pageSize") int pageSize){
        return Result.succ(serverLogDao.getServerLogList(pageStart, pageSize));
    }

    /**
     * 获取全部系统日志，返回在Result的data
     * @return
     */
    @RequestMapping("/usermanagement/getAllServerLogList")
    public Result getAllServerLogList(){
        return Result.succ(serverLogDao.getAllServerLogList());
    }

    /**
     * 获取收件箱大小
     * @return
     */
    @RequestMapping("/usermanagement/getInboxSize")
    public Result getInboxSize(){
        return Result.succ(adminDao.getBoxSize("pop"));
    }

    /**
     * 获取发件箱大小
     * @return
     */
    @RequestMapping("/usermanagement/getOutboxSize")
    public Result getOutboxSize(){
        return Result.succ(adminDao.getBoxSize("smtp"));
    }

    /**
     * 设置收件箱大小
     * @param size
     * @return
     */
    @RequestMapping("/usermanagement/setInboxSize")
    public Result setInboxSize(@RequestParam("size") int size){
        adminDao.setBoxSize("pop",size);
        return Result.succ(null);
    }

    /**
     * 设置发件箱大小
     * @param size
     * @return
     */
    @RequestMapping("/usermanagement/setOutboxSize")
    public Result setOutboxSize(@RequestParam("size") int size){
        adminDao.setBoxSize("smtp",size);
        return Result.succ(null);
    }
}
